package hongcaosp.app.android.search;

import java.util.List;

import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.bean.Pagebean;
import hongcaosp.app.android.modle.bean.UserInfo;
import hongcaosp.app.android.modle.bean.Video;
import hongcaosp.app.android.modle.impl.SearchModleImpl;
import hongcaosp.app.android.modle.mi.SearchModle;
import xlj.lib.android.base.component.BasePresenter;

public class SearchPresenter extends BasePresenter {
    private IListView<Video> videoIListView;

    private IListView<UserInfo> userInfoIListView;

    private SearchModle searchModle;
    private int type;
    private int number;
    private int total;

    public int getNumber() {
        return number;
    }

    public String getContent() {
        return content;
    }

    public SearchPresenter(IListView<Video> videoIListView, IListView<UserInfo> userInfoIListView) {
        this.videoIListView = videoIListView;
        this.userInfoIListView = userInfoIListView;
        searchModle = new SearchModleImpl();
        if (videoIListView != null) {
            type = 1;
        } else {
            type = 2;
        }
    }

    private String content;

    public void fresh(String content) {
        number = 0;
        this.content = content;
        if (type == 1) {
            searchModle.searchVideoResult(UserToken.getDefault().getToken(), content, 20, number, new DataCallBack<Pagebean<Video>>() {
                @Override
                public void onGetData(Pagebean<Video> data) {
                    super.onGetData(data);
                    boolean loadMore = false;
                    total = data.getList() == null ? 0 : data.getList().size();
                    if (data.getTotal() > total) {
                        loadMore = true;
                    }
                    List<Video> videos = data.getList();
                    videoIListView.freshList(videos, loadMore);
                }

                @Override
                public void onFail(int code, String errorMessage) {
                    super.onFail(code, errorMessage);
                    videoIListView.freshList(null, false);
                }
            });
        } else {
            searchModle.searchUserResult(UserToken.getDefault().getToken(), content, 20, number, new DataCallBack<Pagebean<UserInfo>>() {
                @Override
                public void onGetData(Pagebean<UserInfo> data) {
                    super.onGetData(data);
                    boolean loadMore = false;
                    total = data.getList() == null ? 0 : data.getList().size();
                    if (data.getTotal() > total) {
                        loadMore = true;
                    }
                    List<UserInfo> videos = data.getList();
                    userInfoIListView.freshList(videos, loadMore);
                }

                @Override
                public void onFail(int code, String errorMessage) {
                    super.onFail(code, errorMessage);
                    userInfoIListView.freshList(null, false);
                }
            });
        }
    }

    public void loadMore() {
        number ++;
        if (type == 1) {
            searchModle.searchVideoResult(UserToken.getDefault().getToken(), content, 20, number, new DataCallBack<Pagebean<Video>>() {
                @Override
                public void onGetData(Pagebean<Video> data) {
                    super.onGetData(data);
                    boolean loadMore = false;
                    total += data.getList() == null ? 0 : data.getList().size();
                    if (data.getTotal() > total) {
                        loadMore = true;
                    }
                    List<Video> videos = data.getList();
                    videoIListView.addList(videos, loadMore);
                }

                @Override
                public void onFail(int code, String errorMessage) {
                    super.onFail(code, errorMessage);
                    videoIListView.addList(null, false);
                }
            });
        } else {
            searchModle.searchUserResult(UserToken.getDefault().getToken(), content, 20, number, new DataCallBack<Pagebean<UserInfo>>() {
                @Override
                public void onGetData(Pagebean<UserInfo> data) {
                    super.onGetData(data);
                    boolean loadMore = false;
                    total += data.getList() == null ? 0 : data.getList().size();
                    if (data.getTotal() > total) {
                        loadMore = true;
                    }
                    List<UserInfo> videos = data.getList();
                    userInfoIListView.addList(videos, loadMore);
                }

                @Override
                public void onFail(int code, String errorMessage) {
                    super.onFail(code, errorMessage);
                    userInfoIListView.addList(null, false);
                }
            });
        }
    }
}
